﻿using System.Linq.Expressions;
using System.Web.Mvc.Html;
using System.Web.Routing;
using TSharp.Core.Mvc.Extensions;

namespace System.Web.Mvc
{
    /// <summary>
    ///     link扩展
    ///     <para>2010/8/19</para>
    ///     <para>TANGJINGBO</para>
    ///     <para>tangjingbo</para>
    /// </summary>
    public static class LinkExtensions
    {
        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="area">The area.</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink<TController>(this HtmlHelper helper,
                                                            Expression<Action<TController>> action, string linkText,
                                                            string area) where TController : Controller
        {
            return ActionLink(helper, action, linkText, area, null);
        }

        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="area">The area.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink<TController>(this HtmlHelper helper,
                                                            Expression<Action<TController>> action, string linkText,
                                                            string area, object htmlAttributes)
            where TController : Controller
        {
            var route = new RouteValueDictionary();
            route.Add("area", area);
            return ActionLink(helper, linkText, action, route,
                              new RouteValueDictionary(htmlAttributes));
        }

        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="values">The values.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink<TController>(this HtmlHelper helper,
                                                            Expression<Action<TController>> action, string linkText,
                                                            object values, object htmlAttributes)
            where TController : Controller
        {
            return ActionLink(helper, linkText, action, new RouteValueDictionary(values),
                              new RouteValueDictionary(htmlAttributes));
        }

        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="linkText">The link text.</param>
        /// <param name="action">The action.</param>
        /// <param name="values">The values.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static MvcHtmlString ActionLink<TController>(this HtmlHelper helper, string linkText,
                                                            Expression<Action<TController>> action,
                                                            RouteValueDictionary values,
                                                            RouteValueDictionary htmlAttributes)
            where TController : Controller
        {
            RouteValueDictionary routingValues = ExpressionValuesHelper.GetRouteValuesFromExpression(action, values);
            return helper.RouteLink(linkText, routingValues, new RouteValueDictionary(htmlAttributes));
        }
    }
}